package cn.gdpu.course.web.controller.student;

import cn.gdpu.course.constant.Constant;
import cn.gdpu.course.dao.CourseDao;
import cn.gdpu.course.dao.CourseStudentDao;
import cn.gdpu.course.dao.SettingDao;
import cn.gdpu.course.dao.StudentDao;
import cn.gdpu.course.dao.domain.Course;
import cn.gdpu.course.dao.domain.CourseStudent;
import cn.gdpu.course.dao.domain.Setting;
import cn.gdpu.course.dao.domain.Student;

import cn.gdpu.course.web.bean.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2018/3/17.
 */

@Controller
@RequestMapping("/student/choose")
public class StuChooseController {

	@Resource
	private CourseStudentDao courseStudentDao;

	@Resource
	private StudentDao studentDao;

	@Resource
	private CourseDao courseDao;

	@Resource
	private SettingDao settingDao;

	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
	public String index(HttpServletRequest request, ModelMap model) {
		String username = (String) request.getSession().getAttribute(Constant.user_name);
		Student student = new Student();
		student.setSname(username);
		student = studentDao.findBySname(student);
		model.addAttribute("username", username);
		model.addAttribute("studentno", student.getSno());
		return "student/choose";
	}

	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public PageBean<CourseStudent> list(ModelMap model, CourseStudent courseStudent) {
		PageBean<CourseStudent> data = new PageBean<CourseStudent>();

		if (null == courseStudent.getSno() || "".equals(courseStudent.getSno())){
			data.setData(new ArrayList<CourseStudent>());
			return data;
		}
		List<CourseStudent> courseStudents = courseStudentDao.find(courseStudent);
		data.setData(courseStudents);
		return data;
	}

	@ResponseBody
	@RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
	public int save(ModelMap model, CourseStudent courseStudent) {

		/**
		 * 判断此学号是否存在，此功能已废弃
		 */
		Student student = new Student();
		student.setSno(courseStudent.getSno());
		student = studentDao.findBySno(student);
		if (student == null) {
			return -1;
		}

		/**
		 * 判断课程编号是否存在
		 */
		Course course = new Course();
		course.setBno(courseStudent.getBno());
		course = courseDao.findByBno(course);
		if (course == null) {
			return -2;
		}

		/**
		 * 判断课程是否还有名额
		 */
		if (course.getRemain() <= 0) {
			return -3;
		}

		/**
		 * 判断此课程是否已选
		 */
		CourseStudent cs1 = new CourseStudent();
		cs1.setSid(student.getId());
		cs1.setBid(course.getId());
		List<CourseStudent> css1 = courseStudentDao.find(cs1);
		if (!CollectionUtils.isEmpty(css1)) {
			return -4;
		}

		/**
		 * 判断上课时间是否有冲突
		 */
		CourseStudent cs11 = new CourseStudent();
		cs11.setSid(student.getId());
		List<CourseStudent> css11 = courseStudentDao.find(cs11);//通过学生id获得该学生所选的所有课程
		Course course1 = new Course();
		course1.setBno(courseStudent.getBno());
		course1 = courseDao.findByBno(course1);
		for(int i = 0 ; i < css11.size() ; i++) {
			Course course11 = new Course();
			course11.setBno(css11.get(i).getBno());
			course11 = courseDao.findByBno(course11);
//			System.out.println(course11.getCtime());
//			System.out.println(course1.getCtime());
			if (course11.getCtime().equals(course1.getCtime())) {
				return -6;
			}
//			css1.get(i).
		}

		/**
		 * 判断课程选择是否已达上限
		 */
		Setting setting1 = new Setting();
		setting1.setId(Constant.choose_course_max_id);
		setting1 = settingDao.findById(setting1);

		CourseStudent cs2 = new CourseStudent();
		cs2.setSid(student.getId());
		List<CourseStudent> css2 = courseStudentDao.find(cs2);
		if (!CollectionUtils.isEmpty(css2) && css2.size() >= setting1.getValue()) {
			return -5;
		}

//		Setting setting2 = new Setting();
//		setting2.setId(Constant.choose_course_max_id);
//		setting2 = settingDao.findById(setting2);

		courseStudent.setSid(student.getId());
		courseStudent.setBid(course.getId());

		boolean rs1 = courseStudentDao.save(courseStudent);

		Course c = new Course();
		c.setId(course.getId());
		c.setRemain(course.getRemain() - 1);
		boolean rs2 = courseDao.updateRemain(c);

		return rs1 && rs2 ? 1 : 0;
	}

}
